<link type="text/css" href="/admins/js/pagerView/pager-view.css" rel="stylesheet" />
<link type="text/css" href="/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/admins/js/jquery/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="/admins/js/jquery/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript" src="/admins/js/pagerView/pager-view.js"></script>
<script type="text/javascript">
$(function(){
	$.datepicker.setDefaults( $.datepicker.regional[ "zh-CN" ] );
	$( "#add-button").button().click( function () {
		showDialog(0);
	});
	$( ".button").button().click( function () {
		idList = "";
		sp = "";
		$( ".checkbox-del" ).each(function(){
			if ($(this).attr("checked") == "checked") {
				idList += sp + $(this).val();
				if (sp == "") sp = ",";
			}
		});
		if (idList != "") {
			switch (this.id) {
				case 'del-button':
					alertText = '删除';
					reqUrl = '/admin/system/city-del';
					break;
				case 'enable-button':
					alertText = '启用';
					reqUrl = '/admin/system/city-enable';
					break;
				case 'disable-button':
					alertText = '禁用';
					reqUrl = '/admin/system/city-disable';
					break;
				case 'default-button':
					alertText = '默认';
					reqUrl = '/admin/system/city-default';
					break;								
			}
			if (confirm( "你真的要" +alertText+ "选中的地区吗？" )) {
				$.ajax({
					type: "POST",
					url: reqUrl,
					dataType: "json",
					data:"idList="+idList,
					success: function(data) {
						var result=eval(data);
						if (result == true) {
							$( "#queryButton").click();
							alert( alertText + "成功！" );
						} else {
							alert( alertText + "失败！" );
						}
					}
				});
			}
		} else {
			alert("请选择要操作的地区!");
		}
	});
    $( "#queryButton").button().click( function () {
		$("#name2").val($("#name").val());
	    $.ajax({
			type: "POST",
			url: "/admin/system/city-query",
			dataType: "json",
			data:"&name="+$("#name").val()+"&pagesize="+pager.size,
			success: function(data) {
				var items=eval(data.items);
				bindDataTable(items)
				pager.itemCount = data.count;
				pager.index = 1
				pager.render();
			}
		});

		return false; 
	});
	$( "#starttime" ).datepicker();
	$( "#endtime" ).datepicker();
	var pager = new PagerView('pager');
	pager.size = 15; // 每页显示15条记录
	pager.maxButtons = 10;
	pager.itemCount = 0;
	pager.onclick = function(index) {
		$.ajax({
			type: "POST",
			url: "/admin/system/city-pager",
			dataType: "json",
			data:"&name="+$("#name2").val()+"&pagesize="+pager.size+"&pageindex="+index,
			success: function(data) {
				var items=eval(data);
				bindDataTable(items)
			}
		});
		
	};
	pager.render();

	$( "#queryButton").click();
});
function bindDataTable(items) {
	$(".datatable tbody").empty();
	$.each(items, function(k)
	{
		items[k]['default'] = items[k]['default'] == 0 ? '<font color=\'red\'>&times;</font>' : '<font color=\'green\'>&radic;</font>';
		items[k]['state'] = items[k]['state'] == 0 ? '<font color=\'red\'>&times;</font>' : '<font color=\'green\'>&radic;</font>';
		$(".datatable tbody").append("<tr><td><input type=\"checkbox\" value=\""+items[k]['id']+"\" class=\"checkbox-del\" /></td><td>"+items[k]['id']+"</td><td>"+items[k]['name']+"</td><td>"+items[k]['ename']+"</td><td>"+items[k]['serviceFee']+"</td><td>"+items[k]['sortid']+"</td><td>"+items[k]['percent']+"</td><td>"+items[k]['default']+"</td><td>"+items[k]['state']+"</td><td>"+items[k]['parentName']+"</td><td><img src=\"/admins/images/edit.gif\" id=\""+items[k]['id']+"\" class=\"edit-operation\"/></td></tr>");
	});
	$( ".edit-operation").click( function () {
		showDialog(this.id);
	});
	$( ".datatable tbody tr" ).each(function(){
		$(this).click( function () { 
			$( ".datatable .select" ).removeClass("select");
			$(this).addClass("select"); 
		});
		$(this).hover(
			function () {
				$(this).addClass("hover");
			},
			function () {
				$(this).removeClass("hover");
			}
		);
	});
}
</script>
<form action="/admin/system" method="post" style="margin:0px">
<div style=" border:1px solid #DDDDDD; padding:2px; margin:2px;">
<label for="name">名称：</label><input type="text" id="name" name="name" />
<input type="submit" id="queryButton" value="查询"/>
</div>
</form>
<style>
	.datatable {
		text-align: left;
		font-size: 12px;
		font-family: verdana;
		background: #c0c0c0; margin:2px;
	}

	.datatable thead tr,

	.datatable tfoot tr {
		background: #c0c0c0;
	}

	.datatable tbody tr {
		background: #f0f0f0;
	}

	.datatable td, th {
		border: 1px solid white; height:28px;
	}
	.datatable td {background-color:white;}
	
	.datatable .hover td {background-color:#99CCFF;}
	
	.datatable .select td {background-color:#EEEE99;}

	#ui-datepicker-div{ font-size:12px;}
</style>
<div style="padding:2px; margin:2px;">
<table cellspacing="1" width="100%" class="datatable">
	<thead>
		<tr>
            <th></th>
			<th>ID</th>
			<th>地区中文名</th>
			<th>地区英文名</th>
			<th>服务费</th>
			<th>排序</th>
			<th>服务费百分比</th>
			<th>默认</th>
            <th>状态</th>
            <th>所属区域</th>
            <th>操作</th>
		</tr>
	</thead>
    <tfoot>
      <tr><td colspan="12">
        <button id="del-button" class="button">删除选中</button>
        <button id="add-button" >添加</button>
        <button id="enable-button" class="button">启用</button>
        <button id="disable-button" class="button">禁用</button>
        <button id="default-button" class="button">默认</button>
      </td></tr>
    </tfoot>
	<tbody>
	</tbody>
</table>
</div>
<form action="/admin/system/city-pager" method="post">
<input type="hidden" id="name2" name="name2" value="" />
</form>
<div id="pager" class="pager"></div>
<style type="text/css">
#dialog-modal { display:none; }
#dialog-modal input { width:170px;}
</style>
<script type="text/javascript">
$(function(){
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-modal" ).dialog({
		height: 250,
		width: 600,
		modal: true,
		autoOpen: false
	});
    $( "#edit-button-ok").button().click( function () {
		id = $("#city-id").val();cityName = $("#city-name").val();cityEname = $("#city-ename").val();
		cityServiceFee = $("#city-serviceFee").val();cityPercent = $("#city-percent").val();cityState = $("#city-state").val();
		cityDefault = $("#city-default").val();cityType = $("#city-type").val();
		citySortid = $("#city-sortid").val();cityParentId = $("#city-parentId").val();cmd = $("#opcmd").val();
		if (cityParentId > 0) cityParentName = $("#city-parentId").find("option:selected").text();
		else cityParentName = "&nbsp;";
		$.ajax({
			type: "POST",
			url: "/admin/system/city-"+cmd,
			data:"id="+id+"&cityName="+cityName+"&cityEname="+cityEname+"&cityServiceFee="+cityServiceFee+"&cityPercent="+cityPercent+"&cityState="+cityState+"&cityDefault="+cityDefault+"&cityType="+cityType+"&citySortid="+citySortid+"&cityParentId="+cityParentId,
			success: function(data) {
				if (data == 'editok') {
					alert( "更新成功！" );
					row = $( ".datatable .select" ).children();
					$(row).eq(2).text(cityName);
					$(row).eq(3).text(cityEname);
					$(row).eq(4).text(cityServiceFee);
					$(row).eq(5).text(citySortid);
					$(row).eq(6).text(cityPercent);
					cityDefaultHtml = cityDefault == 0 ? '<font color=\'red\'>&times;</font>' : '<font color=\'green\'>&radic;</font>';
					cityStateHtml = cityState == 0 ? '<font color=\'red\'>&times;</font>' : '<font color=\'green\'>&radic;</font>';
					$(row).eq(7).html(cityDefaultHtml);
					$(row).eq(8).html(cityStateHtml);
					$(row).eq(9).html(cityParentName);
					$( "#dialog-modal" ).dialog( "close" );
				} else if (data == 'addok') {
					$( "#queryButton").click();
					$( "#dialog-modal" ).dialog( "close" );
					alert( "添加成功！" );
				} else {
					alert( "操作失败！" );
				}
			}
		});
	});
	$( "#edit-button-cancel").button().click( function () {
		$( "#dialog-modal" ).dialog( "close" );
	});
});
function showDialog(id) {
	if (id > 0) {
		$("#opcmd").val( 'edit' );
		$.ajax({
			type: "POST",
			url: "/admin/system/city-details",
			dataType: "json",
			data:"id="+id,
			success: function(data) {
				var object = eval(data);
				if (object != false) {
					$("#city-id").val( object.id );
					$("#city-name").val( object.name );
					$("#city-ename").val( object.ename );
					$("#city-serviceFee").val( object.serviceFee );
					$("#city-percent").val( object.percent );
					$("#city-state").val( object.state );
					$("#city-default").val( object.cityDefault );
					$("#city-type").val( object.type );
					$("#city-sortid").val( object.sortid );
					$("#city-parentId").val( object.parentId );
					$( "#dialog-modal" ).dialog( "open" );
				} else {
					alert( "获取数据失败！" );
				}
			}
		});
	} else {
		$("#opcmd").val( 'add' );
		$("#city-id").val( '' );
		$("#city-name").val( '' );
		$("#city-ename").val( '' );
		$("#city-serviceFee").val( '' );
		$("#city-percent").val( '' );
		$("#city-state").val( 1 );
		$("#city-default").val( 1 );
		$("#city-type").val( '' );
		$("#city-sortid").val( '' );
		$("#city-parentId").val( '' );
		$( "#dialog-modal" ).dialog( "open" );
	}
}
</script>
<div id="dialog-modal" title="地区操作">
	<table width="100%">
      <tr>
        <td align="right">中文名称：</td><td><input id="city-name" type="text" value="" /></td>
        <td align="right">英文名称：</td><td><input id="city-ename" type="text" value="" /></td>
      </tr>
      <tr>        
        <td align="right">服务费：</td><td><input id="city-serviceFee" type="text" value="" /></td>
        <td width="100" align="right">服务费百分比：</td><td><input id="city-percent" type="text" value="" /></td>
     </tr>
     <tr>
        <td align="right">状态：</td>
        <td>
         <select id="city-state">
          <option value="1">启用</option>
          <option value="0">禁用</option>
         </select>
        </td>
        <td align="right">默认：</td>
        <td>
          <select id="city-default">
            <option value="1">是</option>
            <option value="0">否</option>
          </select>
        </td>
     </tr>
     <tr>
       <td align="right">类型：</td><td><input id="city-type" type="text" value="" /></td>
       <td align="right">排序号：</td><td><input id="city-sortid" type="text" value="" /></td>
     </tr>
     <tr>
        <td align="right">所属区域：</td>
        <td><!--<input id="city-parentId" type="text" value="" />-->
         <select id="city-parentId">
          <option value="0">--无--</option>
          <?php foreach ($this->city_list as $item): ?>
          <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
          <?php endforeach; ?>
         </select>
        </td>
        <td align="right">&nbsp;</td><td>&nbsp;</td>
     </tr>     
     <tr>
       <td colspan="4" align="center">
       <button id="edit-button-ok">确认更新</button>
       <button id="edit-button-cancel">取消返回</button>
       <input type="hidden" id="city-id" value="" />
       <input type="hidden" id="opcmd" value="" />
       </td>
     </tr>
    </table>
</div>